package com.maven.qqspider.ui;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;

import org.apache.log4j.Logger;

import com.maven.qqspider.common.Constant;
import com.maven.qqspider.common.Constant_city;
import com.maven.qqspider.ui.action.SearchAction;
import com.maven.qqspider.ui.widget.CFrame;
import com.maven.qqspider.util.log4j.Log;

public class StandardFrame extends CFrame {

	/** serialVersionUID */
	private static final long serialVersionUID = 1L;

	private Logger logger = Log.getLogger();

	private static StandardFrame standardFrame;
	private SearchAction searchAction = SearchAction.getInstence();

	/** The main panel. */
	private JPanel mainPanel;
	private JTextField cr;
	private JTextField cn;
	private JTextField idol;
	private JTextField game;
	private JTextField it;
	private JTextField sport;
	private JTextField book;
	private JTextField tv;
	private JTextField cloth;
	private JTextField cosm;
	private JTextField place;
	private JTextField interest;
	private JComboBox sex;
	private JComboBox mr;
	private JComboBox at;
	private JComboBox bt;
	private JComboBox month;
	private JComboBox day;
	private JComboBox a1;
	private JComboBox a2;
	private JComboBox pv;
	private JComboBox ct;
	private JComboBox hp;
	private JComboBox hc;
	private JComboBox cap;
	private JComboBox cac;

	private DefaultComboBoxModel pv_Model = new DefaultComboBoxModel();
	private DefaultComboBoxModel ct_Model = new DefaultComboBoxModel();
	private DefaultComboBoxModel hp_Model = new DefaultComboBoxModel();
	private DefaultComboBoxModel hc_Model = new DefaultComboBoxModel();
	private DefaultComboBoxModel cap_Model = new DefaultComboBoxModel();
	private DefaultComboBoxModel cac_Model = new DefaultComboBoxModel();

	private StandardFrame() {
		super();

		// /** set menuBar. */
		// this.setJMenuBar(getAppMenuBar());

		/** set ContentPane. */
		this.setContentPane(getContentPanel());
		mainPanel.setLayout(new MigLayout("", "[][grow][][][][]",
				"[][51.00,baseline][][][][][][][][][][][][][][]"));

		JPanel panel = new JPanel();
		mainPanel.add(panel, "cell 1 1,grow");
		panel.setLayout(new MigLayout("", "[][][grow]", "[][]"));

		JLabel label = new JLabel("URL：");
		label.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		panel.add(label, "cell 1 1,alignx trailing");

		JLabel urlLabel = new JLabel(
				"http://soren.qzone.qq.com/cgi-bin/soren/cgi_userinfo_search");
		urlLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		panel.add(urlLabel, "cell 2 1");

		JPanel panel_1 = new JPanel();
		mainPanel.add(panel_1, "cell 1 3,grow");
		panel_1.setLayout(new MigLayout("", "[][130.00][176.00][][231.00][]",
				"[][][][]"));

		JLabel label_1 = new JLabel("性别：");
		label_1.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		panel_1.add(label_1, "flowx,cell 1 1");

		sex = new JComboBox(Constant.SEX);
		panel_1.add(sex, "cell 1 1");

		JLabel label_18 = new JLabel("年龄：");
		label_18.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		panel_1.add(label_18, "flowx,cell 2 1");

		a1 = new JComboBox(Constant.AGES);
		panel_1.add(a1, "cell 2 1");

		JLabel label_19 = new JLabel("~");
		panel_1.add(label_19, "cell 2 1");

		a2 = new JComboBox(Constant.AGES);
		panel_1.add(a2, "cell 2 1");

		JLabel label_24 = new JLabel("居住地：");
		label_24.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		panel_1.add(label_24, "flowx,cell 4 1");

		JLabel label_30 = new JLabel("故乡：");
		label_30.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		panel_1.add(label_30, "flowx,cell 5 1");

		JLabel label_20 = new JLabel("星座：");
		label_20.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		panel_1.add(label_20, "flowx,cell 1 2");

		at = new JComboBox(Constant.AT);
		panel_1.add(at, "cell 1 2");

		JLabel label_21 = new JLabel("生日：");
		label_21.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		panel_1.add(label_21, "flowx,cell 2 2");

		month = new JComboBox(Constant.MONTHS);
		panel_1.add(month, "cell 2 2");

		JLabel label_22 = new JLabel("月");
		label_22.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		panel_1.add(label_22, "cell 2 2");

		day = new JComboBox(Constant.DAYS_1T031);
		panel_1.add(day, "cell 2 2");

		JLabel label_23 = new JLabel("日");
		label_23.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		panel_1.add(label_23, "cell 2 2");

		initProvinceModel(pv_Model);
		pv = new JComboBox(pv_Model);
		pv.setName("pv");
		pv.addActionListener(new ComboHandler());
		panel_1.add(pv, "cell 4 1");

		JLabel label_25 = new JLabel("省");
		label_25.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		panel_1.add(label_25, "cell 4 1");

		initCityModel(ct_Model);
		ct = new JComboBox(ct_Model);
		panel_1.add(ct, "cell 4 1");

		JLabel label_26 = new JLabel("市");
		label_26.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		panel_1.add(label_26, "cell 4 1");

		JLabel label_27 = new JLabel("婚姻状况：");
		label_27.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		panel_1.add(label_27, "flowx,cell 4 2");

		mr = new JComboBox(Constant.MARRIAGE);
		panel_1.add(mr, "cell 4 2");

		JLabel label_28 = new JLabel();
		label_28.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		panel_1.add(label_28, "cell 4 2");

		JLabel label_29 = new JLabel("血型：");
		label_29.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		panel_1.add(label_29, "cell 4 2");

		bt = new JComboBox(Constant.BT);
		panel_1.add(bt, "cell 4 2");

		initProvinceModel(hp_Model);
		hp = new JComboBox(hp_Model);
		hp.setName("hp");
		hp.addActionListener(new ComboHandler());
		panel_1.add(hp, "cell 5 1");

		JLabel label_31 = new JLabel("省");
		label_31.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		panel_1.add(label_31, "cell 5 1");

		initCityModel(hc_Model);
		hc = new JComboBox(hc_Model);
		panel_1.add(hc, "cell 5 1");

		JLabel label_32 = new JLabel("市");
		label_32.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		panel_1.add(label_32, "cell 5 1");

		JPanel panel_2 = new JPanel();
		mainPanel.add(panel_2, "cell 1 5,grow");
		panel_2.setLayout(new MigLayout("", "[][][][][][]", "[][][]"));

		JLabel label_2 = new JLabel("职业：");
		label_2.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		panel_2.add(label_2, "cell 1 1,alignx trailing");

		cr = new JTextField();
		panel_2.add(cr, "cell 2 1,growx");
		cr.setColumns(10);

		JLabel label_3 = new JLabel("公司名称：");
		label_3.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		panel_2.add(label_3, "cell 3 1,alignx trailing");

		cn = new JTextField();
		panel_2.add(cn, "cell 4 1,growx");
		cn.setColumns(30);

		JLabel label_4 = new JLabel("公司地址：");
		label_4.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		panel_2.add(label_4, "flowx,cell 5 1");

		initProvinceModel(cap_Model);
		cap = new JComboBox(cap_Model);
		cap.setName("cap");
		cap.addActionListener(new ComboHandler());
		panel_2.add(cap, "cell 5 1");

		JLabel label_5 = new JLabel("省");
		label_5.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		panel_2.add(label_5, "cell 5 1");

		initCityModel(cac_Model);
		cac = new JComboBox(cac_Model);
		panel_2.add(cac, "cell 5 1");

		JLabel label_6 = new JLabel("市");
		label_6.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		panel_2.add(label_6, "cell 5 1");

		JPanel panel_3 = new JPanel();
		mainPanel.add(panel_3, "cell 1 7,grow");
		panel_3.setLayout(new MigLayout("",
				"[][][grow][][][][][][][][][grow][][][][][][][][][][grow]",
				"[][][][][][]"));

		JLabel label_7 = new JLabel("喜爱的偶像：");
		label_7.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		panel_3.add(label_7, "cell 1 1,alignx trailing");

		idol = new JTextField();
		panel_3.add(idol, "cell 2 1,growx");
		idol.setColumns(10);

		JLabel label_8 = new JLabel("喜爱的游戏：");
		label_8.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		panel_3.add(label_8, "cell 10 1,alignx trailing");

		game = new JTextField();
		panel_3.add(game, "cell 11 1,growx");
		game.setColumns(10);

		JLabel label_9 = new JLabel("喜爱的数码产品：");
		label_9.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		panel_3.add(label_9, "cell 20 1,alignx trailing");

		it = new JTextField();
		panel_3.add(it, "cell 21 1,growx");
		it.setColumns(10);

		JLabel label_10 = new JLabel("喜爱的运动：");
		label_10.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		panel_3.add(label_10, "cell 1 2,alignx trailing");

		sport = new JTextField();
		panel_3.add(sport, "cell 2 2,growx");
		sport.setColumns(10);

		JLabel label_11 = new JLabel("喜爱的书籍：");
		label_11.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		panel_3.add(label_11, "cell 10 2,alignx trailing");

		book = new JTextField();
		panel_3.add(book, "cell 11 2,growx");
		book.setColumns(10);

		JLabel label_12 = new JLabel("喜爱的电影/电视剧：");
		label_12.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		panel_3.add(label_12, "cell 20 2,alignx trailing");

		tv = new JTextField();
		panel_3.add(tv, "cell 21 2,growx");
		tv.setColumns(10);

		JLabel label_13 = new JLabel("喜爱的品牌：");
		label_13.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		panel_3.add(label_13, "cell 1 3,alignx trailing");

		cloth = new JTextField();
		panel_3.add(cloth, "cell 2 3,growx");
		cloth.setColumns(10);

		JLabel label_14 = new JLabel("喜爱的音乐/乐器：");
		label_14.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		panel_3.add(label_14, "cell 10 3,alignx trailing");

		cosm = new JTextField();
		panel_3.add(cosm, "cell 11 3,growx");
		cosm.setColumns(10);

		JLabel label_15 = new JLabel("去过/想去的地方：");
		label_15.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		panel_3.add(label_15, "cell 20 3,alignx trailing");

		place = new JTextField();
		panel_3.add(place, "cell 21 3,growx");
		place.setColumns(10);

		JLabel label_16 = new JLabel("更多兴趣爱好：");
		label_16.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		panel_3.add(label_16, "cell 1 4,alignx trailing");

		interest = new JTextField();
		panel_3.add(interest, "cell 2 4 20 1,growx");
		interest.setColumns(10);

		JLabel label_17 = new JLabel("（请用空格隔开搜索内容,如:上网聊天 牧羊犬）");
		label_17.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		panel_3.add(label_17, "cell 2 5 20 1");

		JPanel panel_5 = new JPanel();
		mainPanel.add(panel_5, "cell 1 9,grow");
		panel_5.setLayout(new MigLayout("", "[][][grow]", "[][]"));

		JLabel label_33 = new JLabel("输出文件：");
		label_33.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		panel_5.add(label_33, "cell 1 1,alignx trailing");

		JLabel lblDqspiderxls = new JLabel("D:\\QSpider.xls");
		lblDqspiderxls.setForeground(Color.RED);
		lblDqspiderxls.setFont(new Font("Arial", Font.PLAIN, 12));
		panel_5.add(lblDqspiderxls, "flowx,cell 2 1");

		JLabel label_34 = new JLabel("");
		panel_5.add(label_34, "cell 2 1");

		JLabel label_35 = new JLabel("");
		panel_5.add(label_35, "cell 2 1");

		// JLabel label_36 = new JLabel("日志文件：");
		// label_36.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		// panel_5.add(label_36, "cell 2 1");
		//
		// JLabel lblDqspiderlog = new JLabel("D:\\QSpider.log");
		// lblDqspiderlog.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		// lblDqspiderlog.setForeground(Color.RED);
		// panel_5.add(lblDqspiderlog, "cell 2 1");

		JLabel label_38 = new JLabel("");
		panel_5.add(label_38, "cell 2 1");

		JPanel panel_4 = new JPanel();
		mainPanel.add(panel_4, "cell 1 12,grow");
		panel_4.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		JButton searchButton = new JButton("搜索");
		searchButton.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		searchButton.addActionListener(new Button1Handler());
		panel_4.add(searchButton);

		/** application config. */
		config();
	}

	private void initCityModel(DefaultComboBoxModel cModel) {
		for (int i = 0; i < Constant_city.CITY_0.length; i++) {
			cModel.addElement(Constant_city.CITY_0[i]);
		}
	}

	private void initProvinceModel(DefaultComboBoxModel pModel) {
		for (int i = 0; i < Constant_city.PROVINCES.length; i++) {
			pModel.addElement(Constant_city.PROVINCES[i]);
		}
	}

	private void setParams(Map<String, String> pmap) {

		pmap.put("sex", Constant.SEX_MAP.get(standardFrame.sex
				.getSelectedItem()));
		pmap.put("a1", standardFrame.a1.getSelectedItem() == "不限" ? "-1"
				: standardFrame.a1.getSelectedItem().toString());
		pmap.put("a2", standardFrame.a2.getSelectedItem() == "不限" ? "-1"
				: standardFrame.a2.getSelectedItem().toString());
		if (Integer.valueOf(pmap.get("a1")) > Integer.valueOf(pmap.get("a2"))) {
			JOptionPane.showMessageDialog(standardFrame, "年龄区间第二项数值必须大于第一项");
			return;
		}
		pmap.put("pv", Constant_city.PROVINCES_MAP.get(pv_Model
				.getSelectedItem()));
		pmap.put("ct", getReferenceCityMap(pv_Model).get(
				ct_Model.getSelectedItem()));
		pmap.put("hp", Constant_city.PROVINCES_MAP.get(standardFrame.hp
				.getSelectedItem()));
		pmap.put("hc", getReferenceCityMap(hp_Model).get(
				hc_Model.getSelectedItem()));
		pmap.put("at", Constant.AT_MAP.get(standardFrame.at.getSelectedItem()));
		pmap.put("month", standardFrame.month.getSelectedItem() == "不限" ? "-1"
				: standardFrame.month.getSelectedItem().toString());
		pmap.put("day", standardFrame.day.getSelectedItem() == "不限" ? "-1"
				: standardFrame.day.getSelectedItem().toString());
		pmap.put("mr", Constant.MARRIAGE_MAP.get(standardFrame.mr
				.getSelectedItem()));
		pmap.put("bt", Constant.BT_MAP.get(standardFrame.bt.getSelectedItem()));
		pmap.put("cr", standardFrame.cr.getText().trim());
		pmap.put("cn", standardFrame.cn.getText().trim());
		pmap.put("cap", Constant_city.PROVINCES_MAP.get(standardFrame.cap
				.getSelectedItem()));
		pmap.put("cac", getReferenceCityMap(cap_Model).get(
				cac_Model.getSelectedItem()));
		pmap.put("idol", standardFrame.idol.getText().trim());
		pmap.put("game", standardFrame.game.getText().trim());
		pmap.put("it", standardFrame.it.getText().trim());
		pmap.put("sport", standardFrame.sport.getText().trim());
		pmap.put("book", standardFrame.book.getText().trim());
		pmap.put("tv", standardFrame.tv.getText().trim());
		pmap.put("cloth", standardFrame.cloth.getText().trim());
		pmap.put("cosm", standardFrame.cosm.getText().trim());
		pmap.put("place", standardFrame.place.getText().trim());
		pmap.put("interest", standardFrame.interest.getText().trim());
		pmap.put("bSimple", "0");
	}

	private Map<String, String> getReferenceCityMap(DefaultComboBoxModel pModel) {
		String s = Constant_city.PROVINCES_MAP.get(pModel.getSelectedItem());
		return Constant_city.CITY_MAP.get(s);
	}

	private Container getContentPanel() {
		logger.info("---Generating Content Panel.................");

		/** create mainPanel. */
		mainPanel = new JPanel();

		return mainPanel;
	}

	/**
	 * Set the Center Content Pane.
	 * */
	public void setCenterPane(Container centerPane) {
	}

	public void config() {
		// title
		// setTitle(LanguageLoader.getString("cap_app_name"));
		setTitle("Qspider   找朋友 - QQ空间");

		// size
		setSize(900, 500);

		// location
		Dimension screSize = Toolkit.getDefaultToolkit().getScreenSize();
		int x = screSize.width;
		int y = screSize.height;
		int appW = (int) getSize().getWidth();
		int appH = (int) getSize().getHeight();
		// display center
		setLocation((x - appW) / 2, (y - appH) / 2 - 50);
	}

	public static StandardFrame getInstance() {
		if (standardFrame == null) {
			standardFrame = new StandardFrame();
		}
		return standardFrame;
	}

	private class Button1Handler implements ActionListener {
		public void actionPerformed(ActionEvent e) {

			// Set parameters
			Map<String, String> pmap = new HashMap<String, String>();
			setParams(pmap);

			try {
				String[] sArr = searchAction.search(pmap);
				boolean flag = Boolean.valueOf(sArr[0]);
				String msg = sArr[1];

				if (!flag) {
					JOptionPane.showMessageDialog(standardFrame, msg);
				} else {
					JOptionPane.showMessageDialog(standardFrame,
							"QQ号码抓取成功，请打开   \'D:\\QSpider.xls\'   文件查看");
				}
			} catch (MalformedURLException e2) {
				JOptionPane.showMessageDialog(standardFrame, "URL错误");
				e2.printStackTrace();
			} catch (UnsupportedEncodingException e2) {
				JOptionPane.showMessageDialog(standardFrame, "编码错误");
				e2.printStackTrace();
			} catch (IOException e2) {
				JOptionPane.showMessageDialog(standardFrame, "系统超时，请重试！");
				e2.printStackTrace();
			}
		}
	}

	private class ComboHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			JComboBox source = (JComboBox) e.getSource();
			String item = (String) source.getSelectedItem();

			resetCityModel(source, item);
		}

		private void resetCityModel(JComboBox source, String key) {
			String[] city = Constant_city.CITY_NAME_MAP.get(key);
			getReferenceCityModel(source).removeAllElements();
			
			for (int i = 0; i < city.length; i++) {
				getReferenceCityModel(source).addElement(city[i]);
			}
		}
	}

	public DefaultComboBoxModel getReferenceCityModel(JComboBox source) {
		if (source.getName() == "pv") {
			return ct_Model;
		} else if (source.getName() == "hp") {
			return hc_Model;
		} else if (source.getName() == "cap") {
			return cac_Model;
		}

		return null;
	}

}
